<?php
//WSPOLNY INTERFEJS DLA AJAX i WWW

// Define path to application directory
define('APPLICATION_PATH', realpath(dirname(__FILE__)));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/libs/'),
realpath(APPLICATION_PATH . '/Modules/'),
get_include_path(),
)));

require_once 'pgb/Module.php';
require_once 'Zend/Http/CookieJar.php';

session_start();
date_default_timezone_set('Europe/Warsaw');

require_once 'Modules/languages/classes/class.Translate.php';
require_once('classes/class.Html.php');
require_once('classes/class.SQLManager.php');
require_once('classes/class.DBManager.php');
require_once 'classes/Application.php';
require_once 'classes/modules.php';

require_once 'pgb/Event.php';
require_once 'pgb/User/Session.php';

User_Session::init();
Event::init();

function __autoload( $klasa )
{
  $katalogi = array( "classes/");

  foreach( Module::getRegisteredModules() as $registeredModul ){
    $katalogi[] = sprintf("Modules/%s/classes/", $registeredModul);
  }
  $klasa = strtolower( $klasa );
  $arData = array( "class.{$klasa}.php", "{$klasa}.inc", "abstract.{$klasa}.php", "{$klasa}.php");

  foreach( $katalogi as $kat ) {

    $handle = @opendir(APPLICATION_PATH.'/'.$kat);

    if (!is_resource($handle)) {
      throw new Exception(__('Brak katalogu').': '.APPLICATION_PATH.'/'.$kat);
    }

    while (false !== ($file = readdir($handle)))  {
      if(!in_array($file, array(".","..",".dolphinview")) && $file{0} != ".") {
        if(in_array( strtolower( $file ), $arData ) ) {
          require_once("{$kat}{$file}");
          break;
        }
      }
    }

    @closedir($handle);
  }
}

include_once 'Zend/Exception.php';
include_once 'Zend/Config.php';
include_once 'phpinc/Config.php';
include_once 'phpinc/Frontend.php';

//zaladuj dane modulow z pliku konfiguracyjnego zamiast plikow xml
Config::loadConfig(APPLICATION_PATH.'/config/modules.ini');

//ZMIANA USTAWIEN KONFIGURACJI DEBUG ON/OFF
Config::loadConfig(APPLICATION_PATH.'/config/config_dev.ini');
//Config::loadConfig(APPLICATION_PATH.'/config/config.ini');
//$sql = new SQLManager("test","",false);
//echo $sql->getSQLQuery();
include_once 'Zend/Log.php';
include_once 'Zend/Log/Writer/Stream.php';
include_once 'phpinc/Log.php';
include_once 'phpinc/FileUploader.php';
Log::init();


//wlaczone notice'y (masakra!!)
error_reporting(E_ALL^E_NOTICE);
//error_reporting(E_ALL | E_NOTICE);

//dodatkowe logowanie bledow i noticeow php do pliku
ini_set('log_errors', 'On');
ini_set('error_log', sprintf('_files/phperr_%s.txt', date('ymd')));

//Log::logInfo('test info');
//Log::logError('test error');

DBManager::initZendDB();
